using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    private static readonly int IsRight = Animator.StringToHash("isRight");

    public Player player;
    public float WeaponMoveSpeed;//获取Gunshot的武器移动速度
    
    [Header("人物移动")]
    
    private float moveSpeed;//移动速度
    
    private Rigidbody2D rb;
    
    public float moveX;
    public float moveY;
    private Vector2 dir;//移动方向
    [Header("动画控制")]
    private Animator animator;
    void Start()
    {
        animator = GetComponent<Animator>();
        //把movespeed的控制移到player属性里了
        rb = GetComponent<Rigidbody2D>();
        
        GunShoot gunShoot = FindObjectOfType<GunShoot>();
        WeaponMoveSpeed = gunShoot != null ? gunShoot.WeaponMoveSpeed : 1f; // 设置默认值

    }

    
    void Update()
    {
        moveX=Input.GetAxis("Horizontal");
        moveY=Input.GetAxis("Vertical");
    }

    private void FixedUpdate()
    {
        Move();
        AnimationController();
    }

    public void Move()
    {
        WeaponMoveSpeed=FindObjectOfType<GunShoot>().WeaponMoveSpeed;
        moveSpeed = (player.stats.moveSpeed + player.stats.tempBasicMoveSpeed) * WeaponMoveSpeed *
                    player.stats.MoveSpeedMultiplier;
        moveX=Input.GetAxis("Horizontal");
        moveY=Input.GetAxis("Vertical");
        dir=new Vector2(moveX,moveY);
        rb.MovePosition(rb.position + dir * (moveSpeed * Time.fixedDeltaTime));
    }
/// <summary>
/// 控制人物走路动画
/// </summary>
    private void AnimationController()
    {
        float MoveX = Input.GetAxisRaw("Horizontal");
        float MoveY = Input.GetAxisRaw("Vertical");
        // 优先检测 moveX 的变化，优先左右动画
        if (Mathf.Abs(MoveX) > 0f)
        {
            animator.SetBool("isRight", MoveX > 0); // 右
            animator.SetBool("isLeft", MoveX < 0);  // 左
            animator.SetBool("isUp", false);       // 禁止上下动画
            animator.SetBool("isDown", false);
        }
        else if (Mathf.Abs(moveY) > 0f) // moveX 没变化，检测上下动画
        {
            animator.SetBool("isUp", MoveY > 0);   // 上
            animator.SetBool("isDown", MoveY < 0); // 下
            animator.SetBool("isRight", false);    // 禁止左右动画
            animator.SetBool("isLeft", false);
        }
        else
        {
            // 如果没有显著的移动，重置所有动画
            animator.SetBool("isRight", false);
            animator.SetBool("isLeft", false);
            animator.SetBool("isUp", false);
            animator.SetBool("isDown", false);
        }

    }
}
